<template>
  <div class="project-detail-page">
    <div class="project-header-sticky">
      <div class="project-header">
        <button class="back-button" @click="goBack">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <polyline points="15 18 9 12 15 6"></polyline>
          </svg>
          返回
        </button>
        <h1 class="project-title">{{ currentProject.title }}</h1>
      </div>
    </div>
    
    <div class="project-content">
      <div class="project-info">
        <h2>项目介绍</h2>
        <p class="project-description">{{ currentProject.description }}</p>
        
        <h3>项目截图</h3>
        <div class="screenshots-section">
          <div class="screenshot-item" v-for="(screenshot, index) in screenshots" :key="index">
            <div class="screenshot-description">
              <h4>{{ screenshot.title }}</h4>
              <p>{{ screenshot.description }}</p>
            </div>
            <div class="screenshot-image-container" @click="showFullscreenImage(getImageUrl(screenshot.image))">
              <img :src="getImageUrl(screenshot.image)" :alt="screenshot.title" />
            </div>
          </div>
        </div>
        
        <div class="project-actions" v-if="currentProject.url || currentProject.codeUrl">
          <button class="action-button primary" @click="visitProject" v-if="currentProject.url">访问项目</button>
          <button class="action-button secondary" @click="viewCode" v-if="currentProject.codeUrl">查看源码</button>
        </div>
      </div>
    </div>
    
    <!-- 全屏展示图片 -->
    <div class="fullscreen-overlay" v-if="isFullscreen" @click="closeFullscreen">
      <div class="fullscreen-content" @click.stop>
        <img :src="fullscreenImage" alt="Fullscreen image" class="fullscreen-image" />
        <button class="close-button" @click="closeFullscreen">&times;</button>
      </div>
    </div>
  </div>
</template>

<script>
import { projects, defaultProject } from '@/utils/projectData';
import { enableProjectBackground, disableProjectBackground } from '@/utils/projectBackground.js';

export default {
  name: 'ProjectDetail',
  props: {
    project: {
      type: Object,
      default: null
    },
    projectId: {
      type: [String, Number],
      default: null
    },
    id: {
      type: [String, Number],
      default: null
    }
  },
  data() {
      return {
        localProjectId: null,
        isFullscreen: false,
        fullscreenImage: ''
      };
    },
  computed: {
    currentProject() {
      // 如果传入了项目数据，优先使用传入的数据
      if (this.project) {
        return this.project;
      }
      // 如果有路由参数id，则根据id查找项目
      // 优先使用传入的projectId prop，如果没有则使用本地存储的localProjectId
      const projectIdToUse = this.projectId !== null && this.projectId !== undefined ? this.projectId : this.localProjectId;
      if (this.id || projectIdToUse) {
        const id = parseInt(this.id || projectIdToUse);
        const foundProject = projects[id];
        if (foundProject) {
          return foundProject;
        }
      }
      // 否则使用默认项目数据
      return defaultProject;
    },
    // 根据当前项目获取截图数据
    screenshots() {
      return this.currentProject.screenshots || [];
    }
  },
  methods: {
    goBack() {
      // 直接返回首页，与Game.vue中的返回机制保持一致
      this.$router.push('/');
    },
    
    visitProject() {
      if (this.currentProject.url) {
        window.open(this.currentProject.url, '_blank');
      } else {
        alert('该项目还未上线，敬请期待!');
      }
    },
    
    viewCode() {
      if (this.currentProject.codeUrl) {
        window.open(this.currentProject.codeUrl, '_blank');
      }
    },
    
    getImageUrl(imageUrl) {
      // 直接返回图片URL，不再进行路径拼接
      return imageUrl;
    },
    
    // 全屏展示图片
    showFullscreenImage(imageUrl) {
      this.fullscreenImage = imageUrl;
      this.isFullscreen = true;
      // 禁止页面滚动
      document.body.style.overflow = 'hidden';
      // 添加键盘事件监听
      document.addEventListener('keydown', this.handleKeydown);
    },
    
    // 关闭全屏展示
    closeFullscreen() {
      this.isFullscreen = false;
      this.fullscreenImage = '';
      // 恢复页面滚动
      document.body.style.overflow = '';
      // 移除键盘事件监听
      document.removeEventListener('keydown', this.handleKeydown);
    },
    
    // 处理键盘事件，按ESC键关闭全屏
    handleKeydown(event) {
      if (event.key === 'Escape' && this.isFullscreen) {
        this.closeFullscreen();
      }
    }
  },
  mounted() {
    // 启用项目背景渲染
    enableProjectBackground();
    
    if (!this.project && this.$route && this.$route.params) {
      // 如果有路由参数，尝试获取项目ID
      const routeId = this.$route.params.id;
      if (routeId) {
        this.localProjectId = parseInt(routeId);
      }
    }
  },
  
  beforeUnmount() {
    // 禁用项目背景渲染
    disableProjectBackground();
    
    // 组件销毁前移除事件监听
    document.removeEventListener('keydown', this.handleKeydown);
  }
};
</script>

<style scoped>
.project-detail-page {
  min-height: 100vh;
  color: #fff;
  padding: 0 20px 20px 20px;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
}

.project-header-sticky {
  max-width: 1200px;
  position: sticky;
  top: 0;
  z-index: 100;
  background: transparent;
  margin: 0 auto;
  box-shadow: none;
}

.project-header {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  padding: 20px;
  border-radius: 15px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  position: relative;
  min-height: 80px;
  width: 100%; /* 确保容器占满宽度 */
}

.back-button {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: white;
  padding: 10px 20px;
  border-radius: 25px;
  cursor: pointer;
  transition: background 0.3s ease;
  z-index: 10;
  flex-shrink: 0;
}

.back-button:hover {
  background: rgba(255, 255, 255, 0.2);
}

.project-title {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
  font-size: 2rem;
  color: white;
  white-space: nowrap;
}

.project-content {
  max-width: 1200px;
  margin: 0 auto;
}

.project-info {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 15px;
  padding: 30px;
  backdrop-filter: blur(10px);
}

.project-info h2, .project-info h3 {
  color: #4cc9f0;
  margin-bottom: 15px;
}

.project-description {
  font-size: 1.1rem;
  line-height: 1.6;
  margin-bottom: 30px;
  color: rgba(255, 255, 255, 0.9);
}

.screenshots-section {
  margin-top: 30px;
}

.screenshot-item {
  margin-bottom: 40px;
  background: rgba(255, 255, 255, 0.03);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.screenshot-description {
  padding: 20px;
}

.screenshot-description h4 {
  color: #4cc9f0;
  margin-bottom: 10px;
  font-size: 1.3rem;
}

.screenshot-description p {
  color: rgba(255, 255, 255, 0.8);
  line-height: 1.6;
}

.screenshot-image-container {
  cursor: pointer;
  transition: transform 0.3s ease;
  padding: 0 20px 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
  max-height: 500px;
  overflow: hidden;
  position: relative;
}

.screenshot-image-container:hover {
  transform: translateY(-5px);
}

.screenshot-image-container img {
  max-width: 100%;
  max-height: 100%;
  width: 100%;
  height: 100%;
  border-radius: 8px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
  object-fit: contain;
  object-position: center;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
}

.project-actions {
  display: flex;
  gap: 20px;
  margin-top: 30px;
  justify-content: center;
}

.action-button {
  padding: 12px 30px;
  border: none;
  border-radius: 25px;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-button.primary {
  background: linear-gradient(135deg, #4361ee, #3a0ca3);
  color: white;
}

.action-button.secondary {
  background: rgba(255, 255, 255, 0.1);
  color: white;
}

.action-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}

/* 全屏展示样式 */
.fullscreen-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.9);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.fullscreen-content {
  position: relative;
  max-width: 90%;
  max-height: 90%;
}

.close-button {
  position: absolute;
  top: 20px;
  right: 20px;
  background: rgba(0, 0, 0, 0.5);
  border: none;
  color: white;
  font-size: 2rem;
  cursor: pointer;
  z-index: 1001;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.fullscreen-image {
  max-width: 100%;
  max-height: 80vh;
  object-fit: contain;
}

@media (max-width: 768px) {
  .project-header {
    padding: 10px 15px;
    min-height: 80px;
  }
  
  .back-button {
    padding: 8px 15px;
    font-size: 0.9rem;
    z-index: 10; /* 确保在小屏幕上也有较高的z-index */
  }
  
  .back-button svg {
    width: 20px;
    height: 20px;
  }
  
  .project-title {
    font-size: 1.1rem;
    position: relative;
    top: 0;
    left: 0;
    transform: none;
    margin: 0;
    text-align: center;
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    padding: 0 10px;
    align-self: center; /* 确保标题在垂直方向居中 */
  }
  
  .project-info {
    padding: 15px;
  }
  
  .project-actions {
    flex-direction: column;
    gap: 10px;
  }
  
  .action-button {
    width: 100%;
    padding: 10px 20px;
    font-size: 0.9rem;
  }
  
  .close-button {
    top: 10px;
    right: 10px;
    font-size: 1.5rem;
    width: 30px;
    height: 30px;
  }
}

/* 针对非常小的屏幕（如手机竖屏）的额外优化 */
@media (max-width: 480px) {
  .project-header {
    padding: 8px 10px;
    min-height: 45px;
  }
  
  .back-button {
    padding: 6px 12px;
    font-size: 0.8rem;
    z-index: 10; /* 确保在极小屏幕上也有较高的z-index */
  }
  
  .back-button svg {
    width: 18px;
    height: 18px;
  }
  
  .project-title {
    font-size: 1rem;
    padding: 0 5px;
  }
  
  .project-info {
    padding: 12px;
  }
  
  .screenshot-image-container {
    min-height: 200px;
    padding: 0 10px 15px;
  }
  
  .screenshot-description {
    padding: 15px;
  }
  
  .screenshot-description h4 {
    font-size: 1.1rem;
  }
  
  .close-button {
    top: 5px;
    right: 5px;
    font-size: 1.3rem;
    width: 25px;
    height: 25px;
  }
}
</style>